package com.rasou.config;

import brave.http.HttpRequest;
import brave.sampler.SamplerFunction;
import org.springframework.cloud.sleuth.instrument.web.HttpServerSampler;
import org.springframework.cloud.sleuth.instrument.web.SkipPatternProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.regex.Pattern;

@Configuration
public class ZipKinConfig {

    @Bean(name = HttpServerSampler.NAME)
    SamplerFunction<HttpRequest> customHttpRequest(SkipPatternProvider provider) {
        Pattern pattern = provider.skipPattern();
        return request -> {
            String path = request.path();
            boolean shouldSkip = pattern.matcher(path).matches();
            if (shouldSkip) {
                return false;
            }
            return null;
        };
    }
}
